package com.cloud.controller;

import com.cloud.dto.UserDto;
import com.cloud.entity.User;
import com.cloud.service.UserService;
import com.netflix.ribbon.proxy.annotation.Http;
import org.apache.http.client.UserTokenHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.sound.midi.SoundbankResource;
import javax.xml.transform.Source;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("User")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 根据id获取用户
     */
    @GetMapping("/{id}")
    public UserDto getUserById(@PathVariable("id") int userId) {
        return userService.getUerById(userId);
    }

    /**
     * 登录
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/login")
    public Map<?, ?> login(@RequestBody UserDto user) {
        System.out.println(user);
        HashMap<Object, Object> map = new HashMap<>();
        if (user.getName() != null && user.getPassword() != null) {
            UserDto userdto = userService.login(user);
            map.put("user", userdto);
            return map;
        } else {
            return null;
        }
    }

    /**
     * 注册
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/add")
    public Map<?, ?> addUser(@RequestBody UserDto user) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("user", userService.addUser(user));
        return map;
    }


    /**
     * 获取所有用户
     */
    @GetMapping("/all")
    public Map<?, ?> getAll() {
        List<UserDto> userDtos = userService.allUsers();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("users", userDtos);
        return map;
    }


    @GetMapping("/getbyteamid/{teamId}")
    public List<UserDto> getUserByTeamId(@PathVariable("teamId") int teamId) {
        return userService.getUserByTeamId(teamId);
    }

    /**
     * 不需要和前端交换数据
     *
     * @param userid
     * @param teamid
     * @return
     */
    @PostMapping(value = "/accept")
    public UserDto accept(@RequestParam("userId") Integer userid,
                          @RequestParam("teamId") Integer teamid) {
        return userService.accept(userid, teamid);
    }


    @CrossOrigin
    @ResponseBody
    @PostMapping(value = "/update")
    public Map<?, ?> update(@RequestBody User user) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("user", userService.update(user));
        return map;
    }

    @GetMapping(value = "/accept")
    public Map<?, ?> accept() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("users", userService.aUser());
        return map;
    }


}